package drest.service.swivel;

import java.io.IOException;
import java.util.Vector;
import drest.service.*;

public class Swivel {

	/**
	 * @param args
	 */
	
	public static void main(String[] args) throws Exception {
		if (args.length < 2) {
			usage();
			return;
		}
		ArgParser ap = new ArgParser();
		
		try {
			ap.getParser().parse(args);
		} catch (Exception e) {
			System.err.println(e);
			System.exit(2);
		}
		
		Boolean show = ap.getShow();
		Boolean upload = ap.getUpload();
		String[] remaining_args = ap.getParser().getRemainingArgs();
		SwivelService service = new SwivelService(); 

		System.out.println("show: " + show.booleanValue());
		System.out.println("upload: " + upload.booleanValue());
		
		if (show && remaining_args.length > 1) {
			call(service, remaining_args[0], remaining_args[1]);
		} else if (show && remaining_args.length == 1){
			call(service, remaining_args[0], "");
		} else if (upload){
			System.out.println("upload id");
			Vector uargs = ap.populate_upload_args();
			String file_data = ap.getFile((String)uargs.elementAt(1));
			upload(service, file_data, uargs);
		}
		
	}
	
	private static void call(SwivelService service, String data_type, String id) throws IOException{
		String xml = service.getData(data_type, id);
		System.out.println ("--> " + xml);
	}

	private static void upload(SwivelService service, String file_data, Vector upload_data) throws IOException {
		String xml = service.upload(file_data, upload_data);
		System.out.println ("--> " + xml);
	}
	
	
	private static void usage() {
		System.out.println("Usage: java Swivel --show | --upload [options]");
	}

}
